package com.fnzx.util.common;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Wang Zhifu
 * @create 2021/3/18 18:25
 * @Description
 */
public class ResponseUtil {
    /**
     * 返回json格式数据
     * @param httpServletResponse   响应对象
     * @param data 数据对象
     * @throws IOException 网络IO异常
     */
    public static void writeJson(HttpServletResponse httpServletResponse, Object data) throws IOException{
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json; charset=utf-8");
        httpServletResponse.setHeader("Access-Control-Allow-Origin","*");
        httpServletResponse.addHeader("Vary","Origin");
        httpServletResponse.addHeader("Vary","Access-Control-Request-Method");
        httpServletResponse.addHeader("Vary","Access-Control-Request-Headers");
        httpServletResponse.getWriter().write(
                JSON.toJSONString(data)
        );
    }
}
